var cf = require("../../../config.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
	  extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
	  app: app,
	  staticResPath: cf.config.staticResPath,
	  virtualUserNum: 0,
	  virtualZanNum: 0,
	  isLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  wx.hideShareMenu();
	  var that = this;
    wx.setNavigationBarTitle({
      title: app.globalData.cardTitle+'虚拟数据',
    });
	  that.setData({isLoading:true});
      wx.showLoading({title: '加载中...'});
	  app.getUserInfo(this, options, function (userInfo, res) {
	      cusmallToken = wx.getStorageSync('cusmallToken');
	      that.fetchData();
	  });
  },
  
  fetchData: function () {
	var ctx = this;
	wx.request({
		 url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/findVirtualConfig",
		 data: {
			cusmallToken: cusmallToken
		 },
		 header: {
	        'content-type': 'application/json'
	     },
	     success: function (res) {
	    	 wx.hideLoading();
	         var data = res.data;
	         if (0 === data.ret) {
	           var virtualConfig = data.model.virtualConfig;
	           ctx.setData({
	        	   virtualUserNum: virtualConfig.uv,
	        	   virtualZanNum: virtualConfig.thumbs
	           });
	         } else {
	 	       wx.showModal({
	             title: '提示',
	             content: data.msg,
	             showCancel: false
	           });
	         }
	       },
	       fail() {
	         wx.hideLoading();
	       },
	       complete() {
	         ctx.setData({
	           isLoading: false
	         });
	       }
	   });
  },
  
  userNumChange: function (e) {
	  var val = e.detail.value;
	  this.setData({
		  virtualUserNum: val
	  });
  },
  
  userNumBlur: function () {
	  if (! /^([0-9]|[1-9][0-9]{0,7})$/.test(this.data.virtualUserNum)) {
  		wx.showToast({
	        title: '用户浏览数输入范围只能在0~99999999之间',
	        icon: "none"
	    });
	  }
  },
  
  zanNumChange: function (e) {
	  var val = e.detail.value;
	  this.setData({
		  virtualZanNum: val
	  });
  },

  zanNumBlur: function () {
	  if (! /^([0-9]|[1-9][0-9]{0,3})$/.test(this.data.virtualZanNum)) {
  		wx.showToast({
	        title: '点赞数输入范围只能在0~9999之间',
	        icon: "none"
	    });
	  }
  },
  
  virtualCommit: function () {
	  var ctx = this;
	  if (! /^([0-9]|[1-9][0-9]{0,7})$/.test(ctx.data.virtualUserNum)) {
  		wx.showToast({
	        title: '用户浏览数输入范围只能在0~99999999之间',
	        icon: "none"
	    });
	    return;
	  }
	  if (! /^([0-9]|[1-9][0-9]{0,3})$/.test(ctx.data.virtualZanNum)) {
  		wx.showToast({
	        title: '点赞数输入范围只能在0~9999之间',
	        icon: "none"
	    });
	    return;
	  }
	  if (ctx.data.isLoading){
	      wx.showToast({
	        title: '不要重复提交',
	        icon: "none"
	      });
	      return;
	  }
	  ctx.setData({isLoading:true});
	  wx.showLoading({title: '提交中'});
	  wx.request({
		 url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/updateVirtualConfig",
		 data: {
			cusmallToken: cusmallToken,
			uv: ctx.data.virtualUserNum,
			thumbs: ctx.data.virtualZanNum
		 },
		 header: {
	        'content-type': 'application/json'
	     },
	     success: function (res) {
	         var data = res.data;
	         wx.hideLoading()
	         if (data && 0 === data.ret) {
	           wx.showModal({
	             title: '提示',
	             content: '保存成功',
	             showCancel: false,
	             success(){
	               setTimeout(function(){
	                 wx.navigateBack({
	                   delta: 1
	                 });
	               },500);
	             }
	           });
	         } else {
	 	       wx.showModal({
	             title: '提示',
	             content: '保存失败',
	             showCancel: false
	           })
	         }
	       },
	       fail() {
	         wx.hideLoading();
	       },
	       complete() {
	         ctx.setData({
	           isLoading: false
	         });
	       }
	   });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))